// pages/animalDetail/animalDetail.js
Page({
  data: {
    posts: [],
    animalInfo: {},
    options:{},
    AnimalId: 0
  },
  onLoad: function (options) {
    this.setData({
      options:options,
      AnimalId: options.AnimalId,
    })
    var app = getApp();
    wx.request({
      url: 'https://api.fracher21.top/spotit/animalinfo',
      method: "GET",
      data: {
        id: this.data.AnimalId,
        token: app.globalData.token,
      },
      success: (res) => {
        const animalInfo = res.data;
        this.setData({
          animalInfo: animalInfo
        });
      }
    });
    console.log(this.data.AnimalId)
    wx.request({
      url: 'https://api.fracher21.top/spotit/listanimalposts',
      method: "GET",
      data: {
        id: this.data.AnimalId
      },
      success: (res) => {
        const postIdList = res.data.data;
        postIdList.forEach((postId) => {
          this.getPostInfo(postId);
        });
      }
    });
  },

  getPostInfo: function (postId) {
    // 发起获取帖子内容的请求
    var app = getApp();
    wx.request({
      url: 'https://api.fracher21.top/spotit/postinfo',
      method: "GET",
      data: {
        id: postId,
        token: app.globalData.token
      },
      success: (res) => {
        const postInfo = {
          data: res.data,
          id: postId
        }; 

        this.setData({
          posts: this.data.posts.concat(postInfo)
        });
      }
    });
  },



  liketap(e){
    var app = getApp();
    var id = e.currentTarget.dataset.id;

    for(var item of this.data.posts){
      if(item.id==id){
        if(item.data.liked == false){
          wx.request({
            url: 'https://api.fracher21.top/spotit/likepost',
            method: "POST",
            data: {
              token: app.globalData.token,
              id: id
            },
            success: (res) => {
              item.data.liked = true;
              item.data.likes++;
              this.setData({
                posts: this.data.posts,
              })
            }
          })
        }
        else{
          wx.request({
            url: 'https://api.fracher21.top/spotit/unlikepost',
            method: "POST",
            data: {
              token: app.globalData.token,
              id: id
            },
            success: (res) => {
              item.data.liked = false;
              if(item.data.likes)item.data.likes--;
              this.setData({
                posts: this.data.posts,
              })
            }
          })
        }
        break;
      }
    }
  },

  startap(e){
    var app = getApp();
    var id = e.currentTarget.dataset.id;

    for(var item of this.data.posts){
      if(item.id==id){
        if(item.data.stared == false){
          wx.request({
            url: 'https://api.fracher21.top/spotit/starpost',
            method: "POST",
            data: {
              token: app.globalData.token,
              id: id
            },
            success: (res) => {
              item.data.stared = true;
              item.data.stars++;
              this.setData({
                posts: this.data.posts,
              })
            }
          })
        }
        else{
          wx.request({
            url: 'https://api.fracher21.top/spotit/unstarpost',
            method: "POST",
            data: {
              token: app.globalData.token,
              id: id
            },
            success: (res) => {
              item.data.stared = false;
              if(item.data.stars)item.data.stars--;
              this.setData({
                posts: this.data.posts,
              })
            }
          })
        }
        break;
      }
    }
  },

  postDetail: function (e) {
    console.log("跳转详情页")
    var post_id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/posts/postDetail/postDetail?post_id=' + post_id,
    })
  },
})